// pages/login/login.js
let timer = null;
const IsPhone = /^1[34578]\d{9}$/;
var QQMapWX = require('../lib/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
  key: 'O2MBZ-52Z6X-K3Y4F-TGIOG-RFBRQ-MNF7W'
});

Page({

  /**
   * 页面的初始数据
   */
  data: {
    wxsrc: '../image/wx.png',
    codeMsg: "获取验证码",
    time: 10,
    phone: "",
    canClick: true,
    checkImg: '../image/checked.png',
    unCheckImg: '../image/unchecked.png',
    agreeStatus: false,
    code: "",
    uid: "",
    invite_code: "",
    city: "",
    cityOnload: false,
    inType: "",
    shareCode: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    // 获取用户定位
    this.getLocation()

    if (options.inType) {
      that.setData({
        inType: options.inType,
        shareCode: options.invite_code,
      })
    }

    // 加入logout = 1；清除uid，invite_code,退出登录
    if (options.logout && options.logout == 1) {
      wx.clearStorage()
    } else {
      // 获取uid 和invite_code,直接登录
      wx.getStorage({
        key: 'uid',
        success: function(res) {
          that.setData({
            uid: res.data
          })
          wx.getStorage({
            key: 'invite_code',
            success: function(res) {
              that.setData({
                invite_code: res.data
              })
              wx.getStorage({
                key: 'city',
                success: function(res) {
                  var city = res.data;
                  wx.redirectTo({
                    url: `../center/center?city=${city}`,
                  })
                  
                },
              })
            },
          })
        },
        fail(res) {
          // 没有拿到uid 未登录
          console.log(res)
          // 判断是否分享进来的
          if (that.data.inType) {
            that.toRegister(that.data.shareCode)
          }
        }
      })
    }
  },

  getLocation() {
    let that = this;
    wx.getLocation({
      success: function(res) {
        var latitude = res.latitude;
        var longitude = res.longitude;
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: latitude,
            longitude: longitude
          },
          success(res) {
            var city = encodeURI(res.result.address_component.city)
            that.setData({
              city: city
            })
            wx.setStorage({
              key: 'city',
              data: city,
            })
            wx.hideLoading()
            that.setData({
              cityOnload: true
            })
          },

        })
      },
      fail(res) {
        that.setData({
          cityOnload: true
        })
        wx.showToast({
          title: '已拒绝授权定位',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  toWxLogin() {
    let that = this;
    wx.navigateTo({
      url: `../wxLogin/wxLogin?city=${that.data.city}`,
    })
  },

  toRegister(shareCode) {
    if (shareCode) {
      wx.navigateTo({
        url: `../register/register?shareCode=${shareCode}`,
      })
    } else {
      wx.navigateTo({
        url: '../register/register',
      })
    }
    
  },
  // 登录
  handleSubmit() {
    if (!IsPhone.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (!this.data.code) {
      wx.showToast({
        title: '请输入短信验证码',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (!this.data.agreeStatus) {
      wx.showToast({
        title: '请勾选用户服务协议',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    wx.request({
      url: 'https://api.bshlsqs.com/passport/login',
      data: {
        mobile: this.data.phone,
        code: this.data.code,
      },
      success(res) {
        console.log(res)
        let that = this;

        var uid = res.data.data.uid;
        var invite_code = res.data.data.invite_code;
        wx.setStorage({
          key: 'uid',
          data: uid,
        })
        wx.setStorage({
          key: 'invite_code',
          data: invite_code,
        })
        wx.showToast({
          title: '登录成功',
          icon: 'success',
          duration: 2000
        })

        setTimeout(() => {
          wx.redirectTo({
            url: '../center/center?city=${that.data.city}',
          })
        }, 2 * 1000)
      }
    })
  },

  phoneInput(e) {
    var val = e.detail.value
    this.setData({
      phone: val
    })
  },

  codeInput(e) {
    var val = e.detail.value
    this.setData({
      code: val
    })
  },

  agreeFun() {
    this.setData({
      agreeStatus: !this.data.agreeStatus
    })
  },

  getCode() {
    if (!this.data.canClick) {
      return;
    }
    if (!IsPhone.test(this.data.phone)) {

      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    this.data.canClick = false;
    this.countDown();
    this.getSmsCode();
  },

  getSmsCode() {
    wx.request({
      url: 'https://api.bshlsqs.com/passport/send_phone_code',
      data: {
        mobile: this.data.phone,
      },
      success(res) {
        console.log(res.data)
        if (res.data.code == 0) {
          wx.showToast({
            title: '验证码已获取',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  countDown() {
    timer = setTimeout(() => {
      this.data.time -= 1;
      this.setData({
        codeMsg: `剩余${this.data.time}秒`,
      })

      if (this.data.time > 0) {
        this.countDown();
      } else {

        this.setData({
          codeMsg: `重新发送`,
        })
        clearTimeout(timer);
        timer = null;
        this.data.time = 10;
        this.data.canClick = true;
      }
    }, 1 * 1000);
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {

    }
  }
})